package com.kotlin.demo.tools

import androidx.compose.runtime.MutableState
import com.kotlin.demo.bean.DataBean
import com.kotlin.demo.bean.ResBean
import com.kotlin.demo.constant.Constants
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json

/**
 * 多语言路径处理工具类
 */
object ResPathUtils {

    /**
     *获取多语言路径列表
     */
    fun getResList(): MutableList<ResBean> {
        val list = mutableListOf<ResBean>()
        val json = FileUtils.fileToJson()
        if (json.trim() != ""&&json.trim() != "{}") {
            val dataBean = Json.decodeFromString<DataBean>(json)
            if (dataBean.resList.isNotEmpty()) {
                list.addAll(dataBean.resList)
            }
        }
        println(list.toString())
        return list
    }

    /**
     *获取多语言路径列表
     */
    fun getResData(): DataBean {
        val json = FileUtils.fileToJson()
        val bean: DataBean =
            if (json.trim() != ""&&json.trim()!="{}") {
                Json.decodeFromString<DataBean>(json)
            } else {
                DataBean(arrayListOf())
            }
        val localList = bean.resList
        if (localList.isNotEmpty()) {
            bean.resList = localList
        }
        return bean
    }

    /**
     *删除全部多语言路径
     */
    fun delResAll() {
        FileUtils.stringToFile("{}")
    }

    /**
     * 保存多语言路径
     */
    fun saveRes(resTag: String, resPath: String, resListState: MutableState<MutableList<ResBean>>) {
        Constants.mainScope.launch(Dispatchers.IO) {
            val resBean = ResBean(resTag, resPath)
            resListState.value.add(resBean)
        }
    }

}